<?php
/**
 * Created by PhpStorm.
 * User: ZNZG
 * Date: 2020/3/17
 * Time: 15:33
 */

namespace app\api\validate;


use app\common\exception\BaseException;
use think\Validate;

class BaseValidate extends Validate
{
    /**
     * 全局验证层
     * @param string $scene
     * @return bool
     * @throws BaseException
     */
    public function goCheck($scene = '')
    {
        $params = request()->param();

        $check_result = empty($scene)?
            $this->check($params):
            $this->scene($scene)->check($params);

        if(!$check_result)
        {
            throw new BaseException([
                'msg' => $this->getError(),
                'errorCode' => 10000
            ]);
        }

        return true;
    }

    /**
     * 用于验证枚举类型的变量是否在合理范围内
     * @param $value
     * @param $class
     * @return bool
     */
    protected function checkValueIsValid($value,$class)
    {
        $reflection = new \ReflectionClass($class);
        $values = array_values($reflection->getConstants());

        return in_array($value,$values);
    }
}